<?php

class ExceptionHandler implements SplSubject
{
    
    private $observers = array();
    
    public function notify() 
    {
        foreach($this->observers as $obs)
        {
            $obs->update($this);
        }
    }
    
    public function attach(SplObserver $obs) 
    {
        $id = spl_object_hash($obs);
        $this->observers[$id] = $obs;
    }
    
    public function detach(SplObserver $obs)
    {
        $id = spl_object_hash($obs);
        unset($this->observers[$id]);
        
    }
    
    public function handle(Exception $e)
    {
        $this->exception = $e;
        $this->notify();
    }
}

class Logger implements SplObserver
{
    /**
    * Update the error_log with
    * information about the Exception.
    *
    * @param SplSubject $subject   The ExceptionHandler
    * @return boolean
    */
    public function update(SplSubject $subject)
    {
        echo $subject->exception;
    }
}

$handler = new ExceptionHandler();

// Attach an Exception Logger and Mailer
$handler->attach(new Logger());

// Set ExceptionHandler::handle() as the default
set_exception_handler(array($handler, 'handle'));


Class Model
{
    
    protected $data = array();
    
    public function __construct($options) 
    {
        $this->data['tabel'] = get_class($this);
        
        foreach($options as $property => $value)
        {
            if(property_exists($this, $property))
            {
                $this->data[$property] = $value;
            }
        }
    }
    
    public function save($data = array())
    {   
        if($this->data['id'])
        {
            $data['id'] = $this->data['id'];
            return update($data);
        }
    }
    
    public function find()
    {
        $mysqli = new mysqli('127.0.0.1', 'root', '', 'radca');
        
        unset($this->data['tabel']);
        
        $warunki = array();
        
        foreach($this->data as $field => $value)
        {
            $warunki[] = "{$field} = '{$value}'";
        }
        
        $warunki = implode(' AND ',$warunki);
        
        $query = "SELECT * FROM users WHERE {$warunki}";
        
        $result = $mysqli->query($query);
        
        $data = array();
        
        while($row = $result->fetch_assoc())
        {
            $data[] = new $this($row);
        }
        
        return $data;
    }
}

class User extends Model
{
 
    protected $id;
    protected $login;
    protected $password;
    protected $imie;
    protected $nazwisko;
}



function update($data)
{
    $_pola = array();
    
    $template = "UPDATE {$data['tabel']} SET %s WHERE id={$data['id']}";
    
    unset($data['id']);
    unset($data['tabel']);
    
    foreach($data as $column => $value)
    {
        $_pola[] = "{$column}=`{$value}`";
    }
    $pola = implode(',',$_pola); 
    
    echo sprintf($template,$pola);
    echo '<br>';
}






$user = new User(array(
        'login' => 'Master'
        ));


echo '<pre>';
$users = $user->find();

//print_r($users);

foreach($users as $uzytkownik)
{
    $uzytkownik->save(array(
        'imie' => 'costam'
    ));
}

